/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.deliveryticket;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.deliveryticket.service.SaveTicketHeadDomain;
import com.lightkits.wms.dto.deliveryticket.SaveTicketHeadRequestType;
import com.lightkits.wms.dto.deliveryticket.SaveTicketHeadResponseType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * SaveTicketHeadExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/22 17:23
 */
@Component
public class SaveTicketHeadExecutor implements ServiceExecutor<SaveTicketHeadRequestType, SaveTicketHeadResponseType> {
    private final SaveTicketHeadDomain saveTicketHeadDomain;

    @Autowired
    public SaveTicketHeadExecutor(SaveTicketHeadDomain saveTicketHeadDomain) {
        this.saveTicketHeadDomain = saveTicketHeadDomain;
    }

    @Override
    public SaveTicketHeadResponseType service(SaveTicketHeadRequestType request) {
        SaveTicketHeadResponseType response = new SaveTicketHeadResponseType();
        response.setSuccessful(true);
        try {
            CheckResult<Map<String, String>> result = saveTicketHeadDomain.saveTicketHead(request.getHeadDTO());
            if (result.isSuccessful()) {
                response.setTicketId(result.getData().get("ticketHeadId"));
                response.setParentEventId(result.getData().get("eventId"));
            }
            result.fill(response);
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("Save Ticket Lines Error");
        }
        return response;
    }
}
